package com.example.zzh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zzh.entity.ZzhUser;
import com.example.zzh.service.ZzhUserService;
import com.example.zzh.utils.Result;
import com.example.zzh.utils.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 供应商/客户信息表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-07-28
 */
@RestController
@RequestMapping("/zzh-user")
public class ZzhUserController {

    @Autowired
    private ZzhUserService zzhUserService;

    @GetMapping("/list")
    public List<ZzhUser> list(){
        return zzhUserService.list();
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询所有供应商|客户信息分页", notes = "分页")
    public Result list(@RequestBody Map<String,Object> params){
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        Page<ZzhUser> page=new Page<>();
        page.setCurrent((int) params.get("page"));
        page.setSize((int) params.get("limit"));
        LambdaQueryWrapper<ZzhUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage r=null;
        System.out.println(params.get("type"));
        if(!StringUtils.isEmpty(params.get("type"))){
            lambdaQueryWrapper.eq(ZzhUser::getType,params.get("type"));
        }
        if(!StringUtils.isEmpty(params.get("name"))){
            lambdaQueryWrapper.like(ZzhUser::getName,params.get("name"));
        }
        if(!StringUtils.isEmpty(params.get("telephone"))){
            lambdaQueryWrapper.eq(ZzhUser::getTelephone,params.get("telephone"));
        }
        lambdaQueryWrapper.in(ZzhUser::getStatus,1,2);
        r=zzhUserService.pageNames(page,lambdaQueryWrapper);
        return ResultGenerator.genSuccessResult(r.getRecords(),r.getTotal());
    }

    @PutMapping("/updateStatus")
    @ApiOperation(value = "修改供应商|客户信息状态", notes = "修改")
    public Result updateStatus(@RequestParam Long id){
        ZzhUser zzhUser=zzhUserService.getById(id);
        if(!Objects.isNull(zzhUser)){
            if(zzhUser.getStatus()==1){
                zzhUser.setStatus(2);
            }else if(zzhUser.getStatus()==2){
                zzhUser.setStatus(1);
            }
            if(zzhUserService.updateById(zzhUser)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增供应商|客户信息", notes = "新增")
    public Result add(@RequestBody ZzhUser zzhUser){
        if(zzhUserService.save(zzhUser)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"新增不成功");
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改供应商|客户信息", notes = "修改")
    public Result mod(@RequestBody ZzhUser zzhUser){
        if(zzhUserService.updateById(zzhUser)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"修改不成功");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除供应商|客户信息", notes = "删除")
    public Result delete(@RequestParam Long id){
        ZzhUser zzhUser=zzhUserService.getById(id);
        if(!Objects.isNull(zzhUser)){
            zzhUser.setStatus(0);
            if(zzhUserService.updateById(zzhUser)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }
    }

}
